package main

import "fmt"

type ListNode struct {
	Val  int
	Next *ListNode
}

//0ms 100%,3MB 75%
func reversePrint(head *ListNode) []int {
	if head == nil {
		return []int{}
	}
	slice := []int{}
	for head.Next != nil {
		slice = append(slice, head.Val)
		head = head.Next
	}
	slice = append(slice, head.Val)
	sLen := len(slice)
	for i := 0; i < sLen/2; i++ {
		slice[i], slice[sLen-i-1] = slice[sLen-i-1], slice[i]
	}
	return slice
}

//获取链表长度，直接输出切片,0ms 100%, 2.6Mb 97%
func reversePrint1(head *ListNode) []int {
	if head == nil {
		return []int{}
	}
	count := 1
	src := head
	for head.Next != nil {
		count++
		head = head.Next
	}
	slice := make([]int, count)
	for i := 0; i < count; i++ {
		slice[count-i-1] = src.Val
		src = src.Next
	}
	return slice
}

func main() {
	listNode := ListNode{}
	fmt.Println(listNode.Val)
	fmt.Println(reversePrint(nil))
	fmt.Println(reversePrint1(&listNode))
}
